#hmm test

import irc
import string

from commandhandler import CommandHandler
from channels import ChannelManager
import config

import os, sys

class bot(irc.Protocol):
    def __init__(self):
        irc.Protocol.__init__(self)
        server, port = config.servers[0].split(":")
        self.cmd = CommandHandler(self)
        self.chmgr = ChannelManager(self)
        self.connect(server, int(port), config.nicks,
                username=config.username, ircname=config.ircname, version=config.version)
        self.channels = config.channels

        for f in config.plugins:
            if "__init__" in f:
                continue
            
            if "." in f:
                pname = f
            else:
                pname = "plugin." + f

            plugin = __import__(pname, fromlist=[''])
            
            if plugin:
                try:
                    plugin.init(self)
                except AttributeError:
                    del sys.modules[pname]
                    del plugin
 
    def dispatch(self, sender, args):
        self.cmd.dispatch(sender, args)

        """This is going to handle things like numeric replies, nickname
        waterfalling and errors. So it's pretty important to call."""
        irc.Protocol.dispatch(self, sender,args)

if __name__ == "__main__":
    print "bot starting"
    b = bot().start()
    #moo = newtest().start()
